#!/usr/bin/env bash
set -e

# TODO: Only install if needed, and if we install it, remove it when we are done.
xbps-install -y squashfs-tools

# Make the directory that will be extracted
mkdir /extract

# Build/copy all files to extract directory
mksquashfs / /extract/rootfs.squash -e /extract -e /sys -e /proc -e /dev
KERNEL=`find /boot -name "vmlinuz-*"`
INITRAMFS=`find /boot -name "initramfs-*"`
cp $KERNEL /extract/vmlinuz
cp $INITRAMFS /extract/init.img

# Stamp a json file which tells people what files are for what.
json='{"kernel": "vmlinuz", "initramfs": "init.img", "rootfs": "rootfs.squash"}'
echo $json > /extract/image.json